package com.springboot.demo.web;


import com.springboot.demo.entity.BorrowMsg;
import com.springboot.demo.service.BorrowMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
//张智嵩
public class BorrowMsgController {
    @Autowired
    BorrowMsgService borrowMsgService;

    /**
     * 显示所有正在borrowing的申请
     * @param model
     * @return
     */
    @GetMapping("/borrowmsg")
    public String borrowMsgSolving(Model model){
        List<BorrowMsg> borrowMsgs= borrowMsgService.getAllBorrowMsg();

        //放在请求域中
        model.addAttribute("brwmsgs",borrowMsgs);

        return "librarian-borrow";
    }

    /**
     * 借阅成功，将该条申请的状态设为borrowed，即已借阅，并将借阅成功与否设为yes
     * 设置完成后对页面进行重定向（即刷新）
     * @param phone
     * @param bookid
     * @return
     */
    @PostMapping("/setSuc/{phone}/{bookid}/{starttime}")
    public String setSuc(@PathVariable("phone") String phone,@PathVariable("bookid") long bookid,@PathVariable("starttime") String starttime){
        System.out.println("yes");
        borrowMsgService.setSuc(phone,bookid,starttime);
        //借阅成功后，将这本书在book表中的状态改为borrowed
        borrowMsgService.changebooksuc(bookid);
        return "redirect:/borrowmsg";

    }

    /**
     * 借阅失败，将该条申请的状态设为refused，即已拒绝，并将借阅成功与否设为no
     * 设置完成后对页面进行重定向（即刷新）
     * @param phone
     * @param bookid
     * @return
     */
    @PostMapping("/setFail/{phone}/{bookid}/{starttime}")
    public String setFail(@PathVariable("phone") String phone,@PathVariable("bookid") long bookid,@PathVariable("starttime") String starttime){
        borrowMsgService.setFa(phone,bookid,starttime);
        //借阅不成功，将这本书在book表中状态改为not borrow
        borrowMsgService.changebookfail(bookid);
        //由于之前申请时将number+1，因此拒绝后将number-1
        borrowMsgService.readernumminus1(phone);
        return "redirect:/borrowmsg";
    }
}
